/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.FakturaHandler;
import control.Main;
import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

/**
 *
 * @author Morten J, Morten B & Jonas
 */
public class Print extends javax.swing.JFrame {

    private String text;
    private ArrayList<String> page = new ArrayList<>();
    private ArrayList<PrintPage> p = new ArrayList<>();
    private int currentPage = 1;
    private int faktura;

    /**
     * Creates new form Print
     * @param faktura
     */
    public Print(int faktura) {
	this.faktura = faktura;
        initComponents();
        this.getContentPane().setBackground(new Color(186, 153, 108));
    }

    public void setText(String text) {
        this.text = text;
        buildPages();
        setTitle("Museum Sydøstdanmark - Udskriv faktura " + FakturaHandler.faktura.getFakturaNr());
    }

    private int countLines(String text) {
        String[] lines = text.split("\n");
        return lines.length;
    }

    private void buildPages() {
        ArrayList<String> temp = new ArrayList<>(Arrays.asList(text.split("-- ")));
        int tempPage = 0;
        String tempString = "";
        int i = 0;
        while (i < temp.size()) {
            if (tempPage + countLines(temp.get(i)) < 23) {
                tempString = tempString + "-- " + temp.get(i);
                tempPage = tempPage + countLines(temp.get(i));
                i++;
                if (i == temp.size()) {
                    page.add(tempString);
                }
            } else {
                page.add(tempString);
                tempPage = 0;
                tempString = "";
            }
        }
        drawPages();
        showPage(currentPage);
    }

    private void drawPages() {
        for (String k : page) {
            p.add(new PrintPage(k));
        }
        for (PrintPage p1 : p) {
            toPrint.add(p1);
            p1.setSize(400, 500);
            p1.setVisible(false);
        }
    }

    public void printFaktura() {
	try {
	    String filename = "Fakturanummer. " + faktura + ".doc";
	    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " +filename);
	} catch (IOException ex) {
	    GUI.getGuiHandler().setFeedbackMessage("Åbning af faktura til print mislykkedes. Åbn filen manuelt, fra den lokale folder.", Main.getRED_COLOR());
	}
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPage = new javax.swing.JLabel();
        jBtnPrev = new javax.swing.JButton();
        jBtnNext = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        toPrint = new javax.swing.JPanel();
        jBtnPrint = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPage.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jPage.setForeground(new java.awt.Color(250, 250, 250));
        jPage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jPage.setText("1");

        jBtnPrev.setBackground(new java.awt.Color(218, 191, 137));
        jBtnPrev.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jBtnPrev.setForeground(new java.awt.Color(25, 25, 25));
        jBtnPrev.setText("<");
        jBtnPrev.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jBtnPrev.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jBtnPrev.setPreferredSize(new java.awt.Dimension(40, 40));
        jBtnPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnPrevActionPerformed(evt);
            }
        });

        jBtnNext.setBackground(new java.awt.Color(218, 191, 137));
        jBtnNext.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jBtnNext.setForeground(new java.awt.Color(25, 25, 25));
        jBtnNext.setText(">");
        jBtnNext.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jBtnNext.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jBtnNext.setPreferredSize(new java.awt.Dimension(40, 40));
        jBtnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnNextActionPerformed(evt);
            }
        });

        toPrint.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout toPrintLayout = new javax.swing.GroupLayout(toPrint);
        toPrint.setLayout(toPrintLayout);
        toPrintLayout.setHorizontalGroup(
            toPrintLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        toPrintLayout.setVerticalGroup(
            toPrintLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 501, Short.MAX_VALUE)
        );

        jScrollPane2.setViewportView(toPrint);

        jBtnPrint.setBackground(new java.awt.Color(218, 191, 137));
        jBtnPrint.setForeground(new java.awt.Color(25, 25, 25));
        jBtnPrint.setText("Udskriv");
        jBtnPrint.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jBtnPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnPrintActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBtnPrev, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(3, 3, 3)
                        .addComponent(jPage, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBtnNext, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jBtnPrint, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jBtnPrev, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jPage, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jBtnNext, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jBtnPrint, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2))
                .addGap(0, 0, 0))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnPrevActionPerformed
        changePage(0);
    }//GEN-LAST:event_jBtnPrevActionPerformed

    private void jBtnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnNextActionPerformed
        changePage(1);
    }//GEN-LAST:event_jBtnNextActionPerformed

    private void jBtnPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnPrintActionPerformed
        printFaktura();
    }//GEN-LAST:event_jBtnPrintActionPerformed

    private void changePage(int dir) {
        if (dir == 0) {
            if (currentPage > 1) {
                currentPage--;
            }
        } else {
            if (currentPage < page.size()) {
                currentPage++;
            }
        }
        showPage(currentPage);
    }

    private void showPage(int page) {
        page--;
        for (int i = 0; i < p.size(); i++) {
            boolean e = false;
            if (i == page) {
                e = true;
            }
            p.get(i).setVisible(e);
        }
        jPage.setText(currentPage + "/" + this.page.size());
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;

                }
            }
        } catch (ClassNotFoundException ex) {

        } catch (InstantiationException ex) {

        } catch (IllegalAccessException ex) {

        } catch (javax.swing.UnsupportedLookAndFeelException ex) {

        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnNext;
    private javax.swing.JButton jBtnPrev;
    private javax.swing.JButton jBtnPrint;
    private javax.swing.JLabel jPage;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel toPrint;
    // End of variables declaration//GEN-END:variables
}
